package com.yami.shop.api.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.yami.shop.bean.app.dto.*;
import com.yami.shop.bean.app.param.OrderParam;
import com.yami.shop.bean.app.param.OrderPayInfoParam;
import com.yami.shop.bean.app.param.SubmitOrderParam;
import com.yami.shop.bean.dto.ExchangeOrderDTO;
import com.yami.shop.bean.enums.*;
import com.yami.shop.bean.event.EsProductUpdateEvent;
import com.yami.shop.bean.event.SubmitSeckillOrderEvent;
import com.yami.shop.bean.model.*;
import com.yami.shop.bean.param.ChooseCouponParam;
import com.yami.shop.bean.param.PlatformChooseCouponParam;
import com.yami.shop.bean.vo.UserDeliveryInfoVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.Arith;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.delivery.api.manager.DeliveryOrderManager;
import com.yami.shop.manager.*;
import com.yami.shop.manager.impl.ConfirmOrderManager;
import com.yami.shop.manager.impl.ShopCartAdapter;
import com.yami.shop.manager.impl.ShopCartItemAdapter;
import com.yami.shop.personnft.common.model.ExchangeCheckVo;
import com.yami.shop.personnft.common.model.PersonNft;
import com.yami.shop.personnft.common.service.PersonNftService;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @author LGH
 */
@Slf4j
@RestController
@RequestMapping("/p/order")
@Tag(name = "订单接口")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private SubmitOrderManager submitOrderManager;
    @Autowired
    private SkuService skuService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private UserAddrOrderService userAddrOrderService;
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    @Autowired
    private ShopCartAdapter shopCartAdapter;
    @Autowired
    private ShopCartItemAdapter shopCartItemAdapter;
    @Autowired
    private ConfirmOrderManager confirmOrderManager;
    @Autowired
    private ThreadPoolExecutor orderThreadPoolExecutor;
    @Autowired
    private DeliveryOrderManager deliveryOrderManager;
    @Autowired
    private DiscountShopCartManager discountShopCartManager;
    @Autowired
    private ComboShopCartManager comboShopCartManager;
    @Autowired
    private CouponConfirmOrderManager couponConfirmOrderManager;
    @Autowired
    private UserLevelOrderManager userLevelOrderManager;
    @Autowired
    private OrderUseScoreManager orderUseScoreManager;
    @Autowired
    private ShopCustomerService shopCustomerService;
    @Autowired
    private PersonNftService personNftService;
    @Autowired
    private ShopConfig shopConfig;
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private ShopDivisionReceiverService shopDivisionReceiverService;

    @PostMapping("/confirm")
    @Operation(summary =  "结算，生成普通订单信息", description = "传入下单所需要的参数进行下单")
    public ServerResponseEntity<ShopCartOrderMergerDto> confirm(@Valid @RequestBody OrderParam orderParam) throws ExecutionException, InterruptedException {
        String userId = SecurityUtils.getUser().getUserId();
        // 根据userid 去查询是否有未支付的订单
        Integer orderByuserId = orderService.getOrderByuserId(orderParam.getOrderItem().getProdId(), userId);
        if(orderByuserId > 0 ){
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.order.no.payed"));
        }
        Product productById = productService.getProductById(orderParam.getOrderItem().getProdId());
        if (productById == null) {
            return ServerResponseEntity.fail(ResponseEnum.PRODUCT_NOT_EXIST);
        }
        /**
         * 限购数 不等于0 是限购
         */
        if(productById.getMaxNum() != 0) {
            // 大于限购数量
            if (orderParam.getOrderItem().getProdCount() > productById.getMaxNum()) {
                log.info("orderConfirm-{} 您购买数量超出限购数量",userId);
                return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.seckill.buyNum.greater.maxNum"));
            }
            // 小于限购数量
            Integer orderPurchaseCount = orderService.getOrderPurchaseCount(userId, orderParam.getOrderItem().getProdId(),null);
            if (orderPurchaseCount <= productById.getMaxNum()) {
                // 剩余限购数量
                Integer counts = productById.getMaxNum() - orderPurchaseCount;
                if (orderPurchaseCount > counts) {
                    log.info("orderConfirm-{} 您购买数量超出限购数量!",userId);
                    return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.seckill.buyNum.greater.maxNum"));
                }
            }
        }
        // 将要返回给前端的完整的订单信息
        ShopCartOrderMergerDto shopCartOrderMerger = new ShopCartOrderMergerDto();
        shopCartOrderMerger.setIsScorePay(orderParam.getIsScorePay());
        shopCartOrderMerger.setDvyType(orderParam.getDvyType());
        shopCartOrderMerger.setUsableScore(orderParam.getUserUseScore());
        shopCartOrderMerger.setOrderType(OrderType.ORDINARY);
        shopCartOrderMerger.setUserId(userId);
        // 组装获取用户提交的购物车商品项
        List<ShopCartItemDto> shopCartItemsDb = shopCartItemAdapter.getShopCartItemsByOrderItems(orderParam.getOrderItem(), userId, orderParam.getAddrId());
        // 筛选过滤掉不同配送的商品
        List<ShopCartItemDto> shopCartItems = confirmOrderManager.filterShopItemsByType(shopCartOrderMerger, shopCartItemsDb);
        // 该商品不满足任何的配送方式
        if (CollectionUtil.isEmpty(shopCartItems)) {
            return ServerResponseEntity.fail(ResponseEnum.ORDER_DELIVERY_NOT_SUPPORTED, shopCartOrderMerger);
        }
//        ShopCartItemDto firstShopCartItem = shopCartItems.get(0);
        // 是否为预售订单  0 不是
        orderParam.setPreSellStatus(0);
        shopCartOrderMerger.setPreSellStatus(0);
        // 商品类别 0.实物商品 1. 虚拟商品
        int mold = 0;
        if (shopCartItems.stream().filter(shopCartItemDto -> shopCartItemDto.getMold() == 1).count() == shopCartItems.size()) {
            // 订单项中的所有商品都为虚拟商品时，才是虚拟订单
            mold = 1;
        }
        shopCartOrderMerger.setMold(mold);

        // 购物车
        List<ShopCartDto> shopCarts = shopCartAdapter.getShopCarts(shopCartItems);

        // 计算满减，并重新组合购物车
        if (discountShopCartManager != null) {
            shopCarts = discountShopCartManager.calculateDiscountAndMakeUpShopCart(shopCarts);
        }

        // 计算套餐，并重新组合购物车
        if (comboShopCartManager != null) {
            shopCarts = comboShopCartManager.calculateDiscountAndMakeUpShopCart(shopCarts);
        }

        // 异步计算运费，运费暂时和优惠券没啥关联，可以与优惠券异步计算，获取用户地址，自提信息
        CompletableFuture<UserDeliveryInfoVO> deliveryFuture = null;
        if (Objects.equals(mold, 0)) {
            deliveryFuture = CompletableFuture.supplyAsync(
                    () -> deliveryOrderManager.calculateAndGetDeliverInfo(userId, orderParam.getAddrId(), orderParam.getDvyType(), shopCartItems),
                    orderThreadPoolExecutor);
        }


        // 计算优惠券，并返回优惠券信息
        if (couponConfirmOrderManager != null) {
            shopCarts = couponConfirmOrderManager.chooseShopCoupon(new ChooseCouponParam(orderParam.getUserChangeCoupon(), orderParam.getCouponUserIds(), shopCarts));
        }

        // 运费用异步计算，最后要等运费出结果
        UserDeliveryInfoVO userDeliveryInfo = new UserDeliveryInfoVO();
        if (Objects.nonNull(deliveryFuture)) {
            userDeliveryInfo = deliveryFuture.get();
        }

        // 当算完一遍店铺的各种满减活动时，重算一遍订单金额
        confirmOrderManager.recalculateAmountWhenFinishingCalculateShop(shopCartOrderMerger, shopCarts, userDeliveryInfo);

        double orderShopReduce = shopCartOrderMerger.getOrderReduce();

        // ===============================================开始平台优惠的计算==================================================

        // 计算平台优惠券，并返回平台优惠券信息
        if (couponConfirmOrderManager != null) {
            shopCartOrderMerger = couponConfirmOrderManager.choosePlatformCoupon(new PlatformChooseCouponParam(orderParam.getUserChangeCoupon(), orderParam.getCouponUserIds(), shopCartOrderMerger));
        }

        // 等级折扣
        if (userLevelOrderManager != null) {
            userLevelOrderManager.calculateLevelDiscount(shopCartOrderMerger);
        }

        // ===============================================结束平台优惠的计算==================================================
        // 结束平台优惠的计算之后，还要重算一遍金额
        confirmOrderManager.recalculateAmountWhenFinishingCalculatePlatform(shopCartOrderMerger);

        // 计算订单积分抵扣金额
        if (orderUseScoreManager != null) {
            orderUseScoreManager.orderUseScore(shopCartOrderMerger, orderParam, shopCartItems);
        }
        shopCartOrderMerger.setOrderShopReduce(orderShopReduce);
        // 计算平台佣金
        confirmOrderManager.calculatePlatformCommission(shopCartOrderMerger);

        // 缓存计算
        confirmOrderManager.cacheCalculatedInfo(userId, shopCartOrderMerger);
        return ServerResponseEntity.success(shopCartOrderMerger);
    }

    @PostMapping("/submit")
    @Operation(summary =  "提交订单，返回支付流水号", description = "根据传入的参数判断是否为购物车提交订单，同时对购物车进行删除，用户开始进行支付，根据店铺进行拆单")
    public ServerResponseEntity<Map> submitOrders(@Valid @RequestBody SubmitOrderParam submitOrderParam) {

        String userId = SecurityUtils.getUser().getUserId();
        ServerResponseEntity<ShopCartOrderMergerDto> orderCheckResult = submitOrderManager.checkSubmitInfo(submitOrderParam, userId);
        if (!orderCheckResult.isSuccess()) {
            if(StrUtil.equals(ResponseEnum.REPEAT_ORDER.value(),orderCheckResult.getCode())){
                OrderNumbersDto orderNumbersDto = new OrderNumbersDto(null);
                orderNumbersDto.setDuplicateError(1);
                throw new YamiShopBindException("yami.order.expired");
            }
        }
        ShopCartOrderMergerDto mergerOrder = orderCheckResult.getData();
        //查询店铺新生信息
        ShopDetail shopDetailByShopId = shopDetailService.getShopDetailByShopId(mergerOrder.getShopCartOrders().get(0).getShopId());
        if(ObjectUtils.isEmpty(shopDetailByShopId.getShopXsUserId())){
            throw new YamiShopBindException("yami.dtc.newborn.shop.unbound");
        }
        List<ShopCartOrderDto> shopCartOrders = mergerOrder.getShopCartOrders();
        for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
            List<ShopCartItemDiscountDto> shopCartItemDiscounts = shopCartOrder.getShopCartItemDiscounts();
            for (ShopCartItemDiscountDto shopCartItemDiscount : shopCartItemDiscounts) {
                List<ShopCartItemDto> shopCartItems = shopCartItemDiscount.getShopCartItems();
                for (ShopCartItemDto shopCartItem : shopCartItems) {
                    Long prodId = shopCartItem.getProdId();
                    Product productById = productService.getProductById(prodId);
                    if(!productById.getStatus().equals(ProdStatusEnums.NORMAL.getValue())){
                        throw new YamiShopBindException("商品:["+productById.getProdName()+"]已下架");
                    }
                }
            }
        }
        List<Order> orders = orderService.submit(mergerOrder);
        StringBuilder orderNumbers = new StringBuilder();
        Set<Long> prodIds = new HashSet<>();
        for (Order order : orders) {
            orderNumbers.append(order.getOrderNumber()).append(StrUtil.COMMA);
            prodIds.addAll(order.getOrderItems().stream().map(OrderItem::getProdId).collect(Collectors.toSet()));
        }
        orderNumbers.deleteCharAt(orderNumbers.length() - 1);
        // 店铺客户创建
        shopCustomerService.saveCustomerByOrders(orders);
        // 更新es中的商品库存
        // 移除缓存

        // 分账信息集合
        List<LedgerOrderDto> ledgerOrderDtos = new ArrayList<>();
        // 所有商品分账总计
        BigDecimal allDivide = new BigDecimal(BigInteger.ZERO);

        for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
            for (ShopCartItemDiscountDto shopCartItemDiscount : shopCartOrder.getShopCartItemDiscounts()) {
                for (ShopCartItemDto shopCartItem : shopCartItemDiscount.getShopCartItems()) {
                    skuService.removeSkuCacheBySkuId(shopCartItem.getSkuId(), shopCartItem.getProdId());
                    productService.removeProdCacheByProdId(shopCartItem.getProdId());
                    // 计算每个商品的分账信息
                    if(ObjectUtil.isNotNull(shopCartItem.getReceiverGroupId())){
                        List<ShopDivisionReceiver> receivers = shopDivisionReceiverService.list(
                                new LambdaQueryWrapper<ShopDivisionReceiver>()
                                        .eq(ShopDivisionReceiver::getReceiverGroupId, shopCartItem.getReceiverGroupId())
                                        .eq(ShopDivisionReceiver::getShopId, shopDetailByShopId.getShopId())
                                        .eq(ShopDivisionReceiver::getState, 1));
                        for (ShopDivisionReceiver receiver : receivers) {
                            LedgerOrderDto divideAcct = new LedgerOrderDto();
                            divideAcct.setLedgerUserId(receiver.getAccNo());
                            BigDecimal divide = new BigDecimal(String.valueOf(mergerOrder.getTotal())).multiply(receiver.getDivisionProfit()).setScale(2, RoundingMode.HALF_DOWN);
                            divideAcct.setAmount(divide.toString());
                            ledgerOrderDtos.add(divideAcct);
                            allDivide = allDivide.add(divide);
                        }
                    }
                }
            }
        }
        orderService.removeConfirmOrderCache(userId + submitOrderParam.getUuid());
        // 更新es中的商品库存--更新失败打印日志，优先保证订单流程没问题
        try {
            eventPublisher.publishEvent(new EsProductUpdateEvent(mergerOrder.getProdList(), EsOperationType.UPDATE_ORDER_STOCK_NUM_BATCH));
        } catch (Exception e) {
            log.error("提交订单-更新es商品库存失败，错误为：{}", e);
        }

        // 平台佣金分账信息
        LedgerOrderDto merchantToPlatform = new LedgerOrderDto();
        merchantToPlatform.setLedgerUserId(shopConfig.getHnaPay().getMerId());
        merchantToPlatform.setAmount(mergerOrder.getShopCartOrders().get(0).getPlatformCommission().toString());
        ledgerOrderDtos.add(merchantToPlatform);

        // 计算店铺剩余分账金额
        LedgerOrderDto merchantToShop  = new LedgerOrderDto();
        merchantToShop.setLedgerUserId(shopDetailByShopId.getShopXsUserId());
        BigDecimal shopAmount = new BigDecimal(String.valueOf(mergerOrder.getTotal())).subtract(new BigDecimal(String.valueOf(mergerOrder.getShopCartOrders().get(0).getPlatformCommission()))).subtract(allDivide);
        merchantToShop.setAmount(String.valueOf(shopAmount));
        ledgerOrderDtos.add(merchantToShop);

        Map<Object,Object> OrderMap = new HashMap<Object,Object>();
        OrderMap.put("ledgerOrderDtos", JSONUtil.toJsonStr(ledgerOrderDtos));
        OrderMap.put("orderNumbers",orderNumbers);
        return ServerResponseEntity.success(OrderMap);
    }


    @GetMapping("/getOrderPayInfoByOrderNumber")
    @Operation(summary =  "获取订单支付信息", description = "获取订单支付的商品/地址信息")
    @Parameter(name = "orderNumbers", description = "订单流水号", required = true)
    public ServerResponseEntity<OrderPayInfoParam> getOrderPayInfoByOrderNumber(@RequestParam("orderNumbers") String orderNumbers) {
        String userId = SecurityUtils.getUser().getUserId();
        List<String> orderNumberList = Arrays.asList(orderNumbers.split(StrUtil.COMMA));
        //获取订单信息
        List<Order> orderList = orderService.getOrderPayInfoByOrderNumber(orderNumberList);
        List<String> prodNameList = Lists.newArrayList();
        Long addrOrderId = null;
        Date endTime = null;
        int totalScore = 0;
        double totalFee = 0.0;
        boolean isStation = false;
        boolean hasAddr = false;
        Integer status = 1;
        Order orderDb = new Order();
        //获取商品名集合
        for (Order order : orderList) {
            if (!Objects.equals(userId, order.getUserId())) {
                throw new YamiShopBindException("yami.order.no.exist");
            }
            for (OrderItem orderItem : order.getOrderItems()) {
                prodNameList.add(orderItem.getProdName());
                totalScore += orderItem.getUseScore() != null ? orderItem.getUseScore() : 0;
            }
            //第一次循环，获取订单地址id，订单过期时间
            if (Objects.isNull(addrOrderId)) {
                addrOrderId = order.getAddrOrderId();
                if (Objects.equals(2, order.getOrderType())) {
                    // 获取秒杀订单的取消订单时间
                    Integer maxCancelTime = 0;
                    SubmitSeckillOrderEvent event = new SubmitSeckillOrderEvent(order, maxCancelTime);
                    applicationContext.publishEvent(event);
                    maxCancelTime = event.getMaxCancelTime();
                    if (maxCancelTime <= 0) {
                        maxCancelTime = 30;
                    }
                    endTime = DateUtil.offsetMinute(order.getCreateTime(), maxCancelTime);
                } else {
                    // 订单过期时间
                    endTime = DateUtil.offsetMinute(order.getCreateTime(), 30);
                }
            }
            totalFee = Arith.add(totalFee, order.getActualTotal());
            if (Objects.equals(order.getDvyType(), DvyType.STATION.value())) {
                isStation = true;
                orderDb = order;
            }
            if (!Objects.equals(order.getStatus(), OrderStatus.UNPAY.value())) {
                status = order.getStatus();
            }
            if (!hasAddr && !Objects.equals(order.getOrderMold(), 1)) {
                addrOrderId = order.getAddrOrderId();
                hasAddr = true;
            }
        }
        OrderPayInfoParam orderPayInfoParam = new OrderPayInfoParam();
        orderPayInfoParam.setStatus(status);
        orderPayInfoParam.setProdNameList(prodNameList);
        orderPayInfoParam.setEndTime(endTime);
        orderPayInfoParam.setTotalFee(totalFee);
        orderPayInfoParam.setTotalScore(totalScore);
        if (isStation) {
            orderPayInfoParam.setUserAddr("");
            orderPayInfoParam.setReceiver(orderDb.getReceiverName());
            orderPayInfoParam.setMobile(orderDb.getReceiverMobile());
        } else if (hasAddr) {
            //写入商品名、收货地址/电话
            if(ObjectUtils.isNotEmpty(addrOrderId)){
                UserAddrOrder userAddrOrder = userAddrOrderService.getById(addrOrderId);
                String addr = userAddrOrder.getProvince() + userAddrOrder.getCity() + userAddrOrder.getArea() + userAddrOrder.getAddr();
                orderPayInfoParam.setUserAddr(addr);
                orderPayInfoParam.setReceiver(userAddrOrder.getReceiver());
                orderPayInfoParam.setMobile(userAddrOrder.getMobile());
            }
        }
        return ServerResponseEntity.success(orderPayInfoParam);
    }

    /**
     *
     * 兑换订单
     */
    @PostMapping("/exOrder")
    @Operation(summary =  "兑换订单，返回支付流水号", description = "兑换订单")
    public ResponseEntity<Map<String, Object>> exchangeOrder(@RequestBody ExchangeOrderDTO exchangeOrderDTO) {
        String userId = SecurityUtils.getUser().getUserId();
        ExchangeCheckVo exchangeCheckVo = new ExchangeCheckVo();
        exchangeCheckVo.setSpuId(exchangeOrderDTO.getSpuId());
        exchangeCheckVo.setUserId(userId);
        exchangeCheckVo.setCount(exchangeOrderDTO.getCount());
        List<PersonNft> personNftDataList = personNftService.getPersonNftNormalStatusList(exchangeOrderDTO.getPersonIds());
        List<OrderExchangeNft> orderExchangeNfts = new ArrayList<>();
        for(int i=0;i<personNftDataList.size();i++){
            OrderExchangeNft orderExchangeNft = new OrderExchangeNft();
            orderExchangeNft.setDawName(personNftDataList.get(i).getDawName());
            orderExchangeNft.setDawProductCode(personNftDataList.get(i).getDawProductCode());
            orderExchangeNft.setPersonId(personNftDataList.get(i).getId());
            orderExchangeNft.setTokenId(personNftDataList.get(i).getTokenId());
            orderExchangeNft.setShortUrl(personNftDataList.get(i).getShortUrl());
            System.out.println("---+++"+orderExchangeNft.getDawName());
            orderExchangeNfts.add(orderExchangeNft);
        }
        exchangeCheckVo.setOrderExchangeNfts(orderExchangeNfts);
        exchangeCheckVo.setPersonNftIds(exchangeOrderDTO.getPersonIds());
        exchangeCheckVo.setSkuId(exchangeOrderDTO.getSkuId());
        //校验接口
        Map<String, Object> personNftMap = personNftService.exchangeNftCheck(exchangeCheckVo);
        String codes = personNftMap.get("code").toString();
        if(!codes.equals("0")){
            return ResponseEntity.ok(personNftMap);
        }
        exchangeOrderDTO.setOrderExchangeNfts(orderExchangeNfts);
        //生成订单
        String orderNumber = orderService.exSubmit(exchangeOrderDTO,userId);
        //兑换冻结
//        personNftApplyService.exchangeNftFreeze(exchangeOrderDTO.getPersonIds(),orderNumber,userId);
        personNftMap.put("orderNumber",orderNumber);
        return ResponseEntity.ok(personNftMap);
    }



    /**
     *
     * 兑换订单
     */
    @GetMapping("/getOrderInfoByuserId")
    @Operation(summary =  "根据用户查询待支付订单", description = "根据用户查询待支付订单")
    public ServerResponseEntity<Map<String, Object>> getOrderInfoByuserId(@RequestParam("prodId")Long prodId) {
        Map<String, Object> map = new HashMap<String, Object>();
        String userId = SecurityUtils.getUser().getUserId();
        OrderUnpaidInfoDto orderUnpaidInfoDto = orderService.getOrderUnpaidInfoDto(prodId, userId);
        if(ObjectUtils.isEmpty(orderUnpaidInfoDto)){
            map.put("status",false);
            return ServerResponseEntity.success(map);
        }
        Boolean status = true;
        long ordertime= orderUnpaidInfoDto.getCreateTime().getTime()+180000;
        long today= new Date().getTime();
        if(today>ordertime){
            map.put("status",false);
            return ServerResponseEntity.success(map);
        }
        List<LedgerOrderDto> ledgerOrderDtos = new ArrayList<>();
        LedgerOrderDto merchantToPlatform = new LedgerOrderDto();
        merchantToPlatform.setLedgerUserId(shopConfig.getHnaPay().getMerId());
        merchantToPlatform.setAmount(orderUnpaidInfoDto.getPlatformCommission().toString());
        ledgerOrderDtos.add(merchantToPlatform);
        //查询店铺新生信息
        ShopDetail shopDetailByShopId = shopDetailService.getShopDetailByShopId(orderUnpaidInfoDto.getShopId());
        // 店铺
        LedgerOrderDto merchantToShop  = new LedgerOrderDto();
        //店铺分账金额
        BigDecimal shopAmount = new BigDecimal(String.valueOf(orderUnpaidInfoDto.getTotal())).subtract(new BigDecimal(String.valueOf(orderUnpaidInfoDto.getPlatformCommission())));
        merchantToShop.setAmount(String.valueOf(shopAmount));
        //店铺新生id
        merchantToShop.setLedgerUserId(shopDetailByShopId.getShopXsUserId());
        ledgerOrderDtos.add(merchantToShop);
        long timeStamp = orderUnpaidInfoDto.getCreateTime().getTime();
        map.put("ledgerOrderDtos", JSONUtil.toJsonStr(ledgerOrderDtos));
        map.put("productNums",orderUnpaidInfoDto.getProductNums());
        map.put("orderNumber",orderUnpaidInfoDto.getOrderNumber());
        map.put("status",status);
        map.put("createTime",timeStamp);
        return ServerResponseEntity.success(map);
    }
}
